/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */


#include "proxyconfig.h"
#include "src/platform.h"

#include <QtGui/QLineEdit>
#include <QtGui/QCheckBox>
#include <QtGui/QSpinBox>
#include <QtGui/QLabel>
#include <QtGui/QComboBox>
#include <QtGui/QFrame>
#include <QtGui/QGridLayout>
#include <QtGui/QApplication>
#include <QtCore/QSettings>
#include <QtCore/QEvent>
#include <QtNetwork/QNetworkProxy>

//Default values

const QString
ProxyConfig::m_default_hostname = "127.0.0.1",
ProxyConfig::m_default_username = getenv("USER"), // :D
ProxyConfig::m_default_password = "";

const bool
ProxyConfig::m_default_enabled = false;

const int
ProxyConfig::m_default_port = 8118,
ProxyConfig::m_default_type_index = 0;

ProxyConfig::ProxyConfig(QWidget *parent) :
    QWidget(parent)
{
    createUi();    changeEvent(new QEvent(QEvent::LanguageChange));

    connect(enableProxy, SIGNAL(toggled(bool)),proxyFrame, SLOT(setEnabled(bool)));
}

ProxyConfig::~ProxyConfig() {
    delete hostnameLine;
    delete usernameLine;
    delete passwordLine;

    delete enableProxy;

    delete proxyPort;

    delete proxyTypeLabel;
    delete proxyHostLabel;
    delete proxyUsernameLabel;
    delete proxyPasswordLabel;
    delete proxyPortLabel;

    delete proxyTypeCombobox;
    delete proxyFrame;
    delete frameLayout;
    delete mainLayout;
}

void ProxyConfig::save() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("Proxy");

    s.setValue("enabled", enableProxy->isChecked());
    s.setValue("hostname", hostnameLine->text());
    s.setValue("username", usernameLine->text());
    s.setValue("password", passwordLine->text());

    s.setValue("port", proxyPort->value());
    s.setValue("type", proxyTypeCombobox->currentIndex());
    s.endGroup();
}

void ProxyConfig::read() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("Proxy");

    enableProxy->setChecked(s.value("enabled", m_default_enabled).toBool());

    hostnameLine->setText(s.value("hostname", m_default_hostname).toString());
    usernameLine->setText(s.value("username", m_default_username).toString());
    passwordLine->setText(s.value("password", m_default_password).toString());

    proxyPort->setValue(s.value("port", m_default_port).toInt());
    proxyTypeCombobox->setCurrentIndex(s.value("type", m_default_type_index).toInt());
    s.endGroup();
}

void ProxyConfig::truncate() {
    enableProxy->setChecked(m_default_enabled);
    hostnameLine->setText(m_default_hostname);
    usernameLine->setText(m_default_username);
    passwordLine->setText(m_default_password);

    proxyPort->setValue(m_default_port);
    proxyTypeCombobox->setCurrentIndex(m_default_type_index);
}

QNetworkProxy ProxyConfig::getProxy() const {
    return QNetworkProxy
	    (
		(QNetworkProxy::ProxyType)proxyTypeCombobox->itemData(proxyTypeCombobox->currentIndex()).toInt(),
		hostnameLine->text(),
		proxyPort->value(),
		usernameLine->text(),
		passwordLine->text()
	    );
}

bool ProxyConfig::getProxyEnabled() const {
    return enableProxy->isChecked();
}

void ProxyConfig::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
	proxyTypeLabel->setText(QApplication::tr("Proxy type"));
	proxyHostLabel->setText(QApplication::tr("Adress"));
	proxyUsernameLabel->setText(QApplication::tr("Username"));
	proxyPasswordLabel->setText(QApplication::tr("Password"));
	proxyPortLabel->setText(QApplication::tr("Port"));
        enableProxy->setText(QApplication::tr("Enabled"));
    }
}

void ProxyConfig::createUi() {
    hostnameLine = new QLineEdit(this);
    usernameLine = new QLineEdit(this);
    passwordLine =  new QLineEdit(this);
    passwordLine->setEchoMode(QLineEdit::Password);

    enableProxy = new QCheckBox(this);

    proxyPort = new QSpinBox(this);
    proxyPort->setMaximum(100000);

    proxyTypeLabel = new QLabel(this);
    proxyHostLabel = new QLabel(this);
    proxyUsernameLabel = new QLabel(this);
    proxyPasswordLabel = new QLabel(this);
    proxyPortLabel = new QLabel(this);

    proxyTypeCombobox = new QComboBox(this);
    proxyTypeCombobox->addItem("Socks 5 Proxy");
    proxyTypeCombobox->addItem("HTTP Proxy");
    proxyTypeCombobox->addItem("HTTP Caching Proxy");

    proxyTypeCombobox->setItemData(0, QNetworkProxy::Socks5Proxy);
    proxyTypeCombobox->setItemData(1, QNetworkProxy::HttpProxy);
    proxyTypeCombobox->setItemData(2, QNetworkProxy::HttpCachingProxy);

    proxyFrame = new QFrame(this);

    frameLayout = new QGridLayout;
    mainLayout = new QVBoxLayout;

    frameLayout->addWidget(proxyTypeLabel, 0,0);
    frameLayout->addWidget(proxyTypeCombobox, 0, 1);

    frameLayout->addWidget(proxyHostLabel, 1, 0);
    frameLayout->addWidget(hostnameLine, 1, 1);
    frameLayout->addWidget(proxyPortLabel, 1,2);
    frameLayout->addWidget(proxyPort, 1,3);

    frameLayout->addWidget(proxyUsernameLabel, 2, 0);
    frameLayout->addWidget(usernameLine, 2,1);

    frameLayout->addWidget(proxyPasswordLabel, 3,0);
    frameLayout->addWidget(passwordLine, 3, 1);

    proxyFrame->setLayout(frameLayout);

    mainLayout->addWidget(enableProxy);
    mainLayout->addWidget(proxyFrame);
    mainLayout->addStretch();

    this->setLayout(mainLayout);
}


